<?php

class SendVacNotification
{
    public static function sendNotification($vacUsername, $vacTitle, $vacMessage)
    {
        /*
        ACS Push Notification Web Service
        */
        /*** SETUP ***************************************************/
//        $key = "Igam1o4Z4z1qdyOYKkmQA5tFr08poYtL"; // Production key.
        $key = "nSssajVkPNQBP9ZHEWLXRr8zfG8pWX0W"; // Development key.
        $username   = 'eric.1012@hotmail.com';
        $password   = 'eric@1012';
        $to_ids     = 'everyone';
        $channel    = $vacUsername;   // this channel is vac user login name, i use this is because can specific user to get notification
        $message    = $vacMessage;
        $title      = $vacTitle;
        $tmp_fname  = 'cookie.txt';
        $json       = '{"alert":"'. $message .'","title":"'. $title .'","vibrate":true,"sound":"default","icon":"appicon"}';

        if (!is_null($key) && !is_null($username) && !is_null($password) && !is_null($channel) && !is_null($message) && !is_null($title)){
            /*** PUSH NOTIFICATION ***********************************/
            $post_array = array('login' => $username, 'password' => $password);

            /*** INIT CURL *******************************************/
            $curlObj    = curl_init();
            $c_opt      = array(CURLOPT_URL => 'https://api.cloud.appcelerator.com/v1/users/login.json?key='.$key,
                CURLOPT_COOKIEJAR => $tmp_fname,
                CURLOPT_COOKIEFILE => $tmp_fname,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS  =>  "login=".$username."&password=".$password,
                CURLOPT_FOLLOWLOCATION  =>  1,
                CURLOPT_TIMEOUT => 60);
            /*** LOGIN **********************************************/
            curl_setopt_array($curlObj, $c_opt);
            $session = curl_exec($curlObj);

            /*** SEND PUSH ******************************************/
            $c_opt[CURLOPT_URL]         = "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=".$key;
            $c_opt[CURLOPT_POSTFIELDS]  = "channel=".$channel."&payload=".$json."&to_ids=".$to_ids;

            curl_setopt_array($curlObj, $c_opt);
            $session = curl_exec($curlObj);

            /*** THE END ********************************************/
            curl_close($curlObj);
//            header('Content-Type: application/json');
//            die(json_encode(array('response' => json_decode($session))));
        }
    }
}